package org.ucas.faker.web.service.graph;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ucas.faker.web.dao.SpecialKeywordDao;
import org.ucas.faker.web.entity.SpecialKeyword;
import org.ucas.faker.web.model.KeywordNode;

@Service
@Transactional
public class SpecialKeywordService {
	@Autowired
	SpecialKeywordDao skDao;
	
	public SpecialKeywordService() {
		// TODO Auto-generated constructor stub
	}
	
	public List<List<String> > getBranchAndEffectBySpTree(Long treeId) {
		List<SpecialKeyword> list = skDao.findByTreeId(treeId);
		Queue<KeywordNode> queue = new LinkedList<>();
		List<KeywordNode> rootlist = new LinkedList<>();
		KeywordNode root = null;
		
		List<SpecialKeyword> tmplist = new ArrayList<>(list.size()); 
		//System.out.println(list.size());
		for (SpecialKeyword sk : list) {
			//System.out.println(sk.getKeywordValue());			
			if (sk.getParentId() == 0) {
				root = new KeywordNode(sk.getId(), sk.getKeywordValue(), null);
				// queue.add(root);
				//list.remove(sk);
				rootlist.add(root);
				// break;
			}else{
				tmplist.add(sk);
			}
		}
		
		list.clear();
		list.addAll(tmplist);

		//System.out.println(rootlist.size()+ "  ##  " + tmplist.size());
		
		for (KeywordNode node : rootlist) {
			queue.add(node);
			while (queue.size() > 0) {
				KeywordNode kt = queue.remove();
				List<SpecialKeyword> rmlist = new LinkedList<>();
				for (SpecialKeyword sk : list) {
					if (sk.getParentId() == kt.getId().intValue()) {
						if (kt.getChild() == null)
							kt.setChild(new LinkedList<>());
						KeywordNode kn = new KeywordNode(sk.getId(), sk.getKeywordValue(), null);
						kt.getChild().add(kn);
						queue.add(kn);
						// 处理完从结果删除
						rmlist.add(sk);
					}
				}
				for (SpecialKeyword sk : rmlist) {
					list.remove(sk);
				}
			}
		}
		
	/*	for (KeywordNode node : rootlist){
			System.out.println("traverse");
			KeywordNode.traverse(node);
		}
		*/
		List<String> branch = new LinkedList<>();
		List<String> effect = new LinkedList<>();
//		System.out.println("Get Branch And Effect");
		KeywordNode.GetBranchAndEffect(rootlist,branch,effect);
//		for(String s:branch){
//			System.out.println(s);
//		}System.out.println("\n\n");
//		for(String s:effect){
//			System.out.println(s);
//		}
		List<List<String> > res = new LinkedList<>();
		res.add(branch);
		res.add(effect);
		
		return res;
	}

	//

	public List<KeywordNode> getSpecialKeywordTree(long tree_id){
		List<SpecialKeyword> list = skDao.findByTreeId(tree_id);
		Queue<KeywordNode> queue = new LinkedList<>();
		List<KeywordNode> rootlist = new LinkedList<>();
		KeywordNode root = null;
		
		List<SpecialKeyword> tmplist = new ArrayList<>(list.size()); 
		//System.out.println(list.size());
		for (SpecialKeyword sk : list) {
			//System.out.println(sk.getKeywordValue());			
			if (sk.getParentId() == 0) {
				root = new KeywordNode(sk.getId(), sk.getKeywordValue(), null);
				// queue.add(root);
				//list.remove(sk);
				rootlist.add(root);
				// break;
			}else{
				tmplist.add(sk);
			}
		}
		
		list.clear();
		list.addAll(tmplist);

		//System.out.println(rootlist.size()+ "  ##  " + tmplist.size());
		
		for (KeywordNode node : rootlist) {
			queue.add(node);
			while (queue.size() > 0) {
				KeywordNode kt = queue.remove();
				List<SpecialKeyword> rmlist = new LinkedList<>();
				for (SpecialKeyword sk : list) {
					if (sk.getParentId() == kt.getId().intValue()) {
						if (kt.getChild() == null)
							kt.setChild(new LinkedList<>());
						KeywordNode kn = new KeywordNode(sk.getId(), sk.getKeywordValue(), null);
						kt.getChild().add(kn);
						queue.add(kn);
						// 处理完从结果删除
						rmlist.add(sk);
					}
				}
				for (SpecialKeyword sk : rmlist) {
					list.remove(sk);
				}
			}
		}
		
		return rootlist;
		/*
		for (KeywordNode node : rootlist){
			System.out.println("traverse");
			KeywordNode.traverse(node);
		}
		*/
		
	}

}
